<?php

/**
* alltosun.com 前台文章控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-15 下午2:19:50 $
* $Id$
*/

class Action
{
    public function __call($action = "", $param = array())
    {
        $id = (int)$action;
        if (!$id) {
            return '请选择正确的信息';
        }

        $article_info   = _uri('article', (int)$id);

        if (!$article_info || $article_info['status'] != 1) {
            return '该文章不存在或被删除';
        }

        if($article_info['alias'] && !article_helper::url_is_rewrited()) {
            Response::redirect(AnUrl($article_info['alias']));
            return;
        }

        Response::assign('article_id', $id);
        Response::assign('article_info', $article_info);
        display_template('article_info.html');
    }

    public function index()
    {
    }

    public function loadData()
    {
        $params['page_no']     = Request::getParam('page_num', 1);
        $params['category_id'] = Request::getParam('category_id', 0);

        if (isset($params['category_id']) && $params['category_id']) {
            $filter['category_id'] = $params['category_id'];
        }
        $params['inherit'] = 1;
        $filter['status'] = 1;
        $total = _model('article')->getTotal($filter);

        if ($total) {
            $article_list = _widget('article')->get_article_list($params);
            return array(
                    'info'  => 'ok',
                    'count' => count($article_list),
                    'total' => $total,
                    'data'  => $article_list,
            );
        }

        return '当前没有任何资讯内容';
    }
    
    
    /**
     * 显示帮助详情
     */
    public function help_detail()
    {
        $id = Request::getParam('id', 0);
        
        if (!$id) {
            Response::assign('error_info', '没找到帮助详情id');
            Response::display('error.html');
            return ;
        }
        
        $help_info = _uri('article', $id);
        
        if (!$help_info) {
            Response::assign('error_info', '没有此帮助详情信息!');
            Response::display('error.html');
            return ;
        }
        
//         $video_content = '';
//         if ($help_info['video_link']) {
//             $video_content = '<video id="my-video" class="video-js" controls preload="auto" width="100%" height="auto" poster="MY_VIDEO_POSTER.jpg" data-setup="{}"><source src="'.$help_info['video_link'].'" type="video/mp4"><p class="vjs-no-js">抱歉您的浏览器不支持此视频格式</p></video>';
//         }
//         $content = htmlspecialchars_decode($help_info['content']);
        
//         Response::assign('content',$video_content.$content);
        $time_info = explode(' ', $help_info['add_time']);
        $help_info['add_time'] = $time_info;
        
        Response::assign('help_info', $help_info);
        Response::display('m/article_info.html');
    }
    
    
    /**
     * 显示具体分类下的列表
     */
    public function category_list()
    {
        $id = Request::getParam('id', 0);
        if (!$id) {
            Response::assign('error_info', '没找到帮助类型id!');
            Response::display('error.html');
            return ;
        }
        
        $category_info = _uri('category', $id);
        if (!$category_info) {
            Response::assign('error_info', '没找到帮助类型信息!');
            Response::display('error.html');
            return ;
        }
        
        $article_list = _model('article')->getList(array('category_id'=>$id, 'status'=>1), 'ORDER BY `view_order` DESC');
        if ($article_list) {
            foreach ($article_list as $k => $v) {
                $article_list[$k]['link'] = AnUrl('article/help_detail?id='.$v['id']);
            }
        }
        
        Response::assign('title', $category_info['title']);
        Response::assign('article_list', $article_list);
//         var_dump($article_list);die;
        // @TODO
        Response::display('m/list.html');
        
        
    }
    
    
    /**
     * 帮助页
     */
    public function category()
    {
        $type = Request::getParam('type', '2');
        if (!$type || !is_numeric($type)) {
            Response::assign('error_info', '参数错误!');
            Response::display('error.html');
            return ;
        }
        
        $filter = array();
        $filter['parent_id'] = 6;
        $filter['status']    = 1;
        $filter['type']      = $type;
        
        // 获取分类
        $category_list = _model('category')->getList($filter, 'ORDER BY `view_order`');
        
        if ($category_list) {
            foreach ($category_list as $k=>$v) {
                // 15代表积分相关 16代表新手入门 17代表常见问题 18代表其他问题(商户端)
                if (15 != $v['id'] && 16 != $v['id'] && 17 != $v['id'] && 18 != $v['id']) {
                    // 获取每条分类下的对应的帮助列表
                   $help_list = _model('article')->getList(array('category_id'=>$v['id'], 'type'=>$type), 'ORDER BY `view_order` DESC LIMIT 0,4');
                } elseif (18 == $v['id']) {
                    // 获取每条分类下的对应的帮助列表
                    $help_list = _model('article')->getList(array('category_id'=>$v['id'], 'type'=>$type), 'ORDER BY `view_order` DESC LIMIT 0,6');
                } else {
                    if (16 == $v['id'] || 17 == $v['id']) {
                        $help_list = _model('article')->getList(array('category_id'=>$v['id'], 'type'=>$type), 'ORDER BY `view_order` DESC');
                    } else {
                        $help_list = _model('article')->getList(array('category_id'=>$v['id'], 'type'=>$type), 'ORDER BY `view_order` DESC LIMIT 0,2');
                    }
                }
                
                $category_list[$k]['link'] = AnUrl('article/category_list?id='.$v['id']);
                $category_list[$k]['help_list'] = array();
                if ($help_list) {
                    
                    foreach ($help_list as $key => $val) {
                        $help_list[$key]['link'] = AnUrl('article/help_detail?id='.$val['id']);
                    }
                    $category_list[$k]['help_list'] = $help_list;
                    // $category_list[$k]['video_link'] = htmlspecialchars_decode($val['video_link']);
                }
                
                
            }
        }

        Response::assign('title', '帮助中心');
        Response::assign('category_list', $category_list);
        // 商户端
        if ('1' == $type) {
            Response::assign('relation', article_config::$store_relation);
            Response::display('m/store_category.html');
        } elseif ('2' == $type) {
            Response::assign('relation', article_config::$manage_relation);
            Response::display('m/category.html');
        }
        
        
    }
    
    
    
}
?>